package com.mercury.core.config;

import com.mercury.core.config.properties.RedissonProperties;
import org.apache.commons.lang3.StringUtils;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.redisson.config.SingleServerConfig;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author mercury
 */
@Configuration
@EnableConfigurationProperties(RedissonProperties.class)
public class RedissonAutoConfiguration {


    @Bean
    public Config redissonConfig(RedissonProperties redissonProperties) {
        Config config = new Config();
        SingleServerConfig singleServerConfig = config.useSingleServer();
        String address = redissonProperties.getAddress();
        String password = redissonProperties.getPassword();
        singleServerConfig.setAddress(address);
        if (StringUtils.isNotEmpty(password)) {
            singleServerConfig.setPassword(password);
        }
        return config;
    }

    @Bean
    public RedissonClient redissonClient(RedissonProperties redissonProperties) {
        return Redisson.create(redissonConfig(redissonProperties));
    }
}
